from flask import Blueprint,request,make_response,jsonify
order=Blueprint('/order',__name__)
from model.model import Hoser, db, Order
import time
from Redis import temp_cache


@temp_cache.cached(timeout=10)

@order.route('/order')
def Order_Get():
    home=Order.query.filter_by().all()
    aaa=[]
    for i in home:
        aaa.append({
            'id':i.id,
            "title":i.title,
            "price":i.price,
            "houser": i.houser,
            "status": i.status
        })

    return jsonify(code=200,msg=aaa)



@order.route('/order',methods=['post'])
def Order_Add():
    data=request.json
    data.pop('pid')
    home=Order(**data)
    db.session.add(home)
    db.session.commit()
    return jsonify(code=200,msg=data)


@order.route('/order',methods=['put'])
def Order_Put():
    data=request.json
    p_id=data.pop('pid')
    # print(p_id)
    title=data.get('title')
    price=data.get('price')
    houser=data.get('houser')
    status=data.get('status')

    Order.query.filter_by(id=p_id).update({'title':title,"price":price,'houser':houser,'status':status})
    db.session.commit()
    return jsonify(code=200,msg='成功')
#
@order.route('/order',methods=['delete'])
def Order_Delete():
    p_id=request.json.get('pid')
    home=Order.query.filter_by(id=p_id).first()
    db.session.delete(home)
    db.session.commit()
    return jsonify(code=200,msg='成功')





